package com.beiding.service.impl;

import com.beiding.dao.EvaluationDao;
import com.beiding.pojo.Evaluation;
import com.beiding.service.EvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EvaluationServiceImpl implements EvaluationService {

    private EvaluationDao evaluationDao;

    @Autowired
    public void setEvaluationDao(EvaluationDao evaluationDao) {
        this.evaluationDao = evaluationDao;
    }

    @Override
    @Secured("ROLE_TENANT")
    public String evaluate(Evaluation evaluation) {


        String valuer = evaluation.getValuer();

        //取出前两位和最后两位
        StringBuilder stringBuilder = new StringBuilder(valuer.substring(0, 2));

        String end = valuer.substring(valuer.length() - 2);

        for (int i = 0; i < valuer.length() - 4; i++) {
            stringBuilder.append("*");
        }
        stringBuilder.append(end);
        evaluation.setEncryptValuer(stringBuilder.toString());

        evaluationDao.save(evaluation);
        return evaluation.getId();
    }

    @Override
    @Secured("ROLE_TENANT")
    public List<Evaluation> get(String id, Pageable pageable) {
        return evaluationDao.findByHolderInPage(id, pageable, "evaluation", "encryptValuer", "service", "environment");
    }



}
